package codigoWeb;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ViewObjects.ComparativaView;

/**
 * Servlet implementation class consCompCodigoFabricanteyMarca
 */
public class consCompCodigoFabricanteyMarca extends HttpServlet {
	private Connection con;
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public consCompCodigoFabricanteyMarca() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String codigoFabricante = request.getParameter("codigoFabricante");
		String marca = request.getParameter("marca");
		String usuario = "funfun"; String password = "funfun";
		ArrayList<ComparativaView> comparativas = new  ArrayList<ComparativaView>();
		ComparativaView comp = null;
		try {
				
			Class.forName("net.sourceforge.jtds.jdbc.Driver");
			String connectionUrl = "jdbc:jtds:sqlserver://localhost:1433/macaca;";
			con = DriverManager.getConnection(connectionUrl,usuario,password);
			System.out.println(con.getCatalog());
			PreparedStatement ps = con.prepareStatement("select idProveedor, idLista, codigoSFK, codigoFabricante, paisOrigen, marca, caracteristicas, precioCalculado, stockDisp from dbo.Comparativa where codigoFabricante=? and marca=?");
			ps.setString(1,codigoFabricante);
			ps.setString(2,marca);
			
			ResultSet rs = ps.executeQuery();
				while(rs.next())
				{
					comp = new ComparativaView();
					comp.setIdProveedor(rs.getInt(1));
					comp.setIdLista(rs.getInt(2));
					comp.setCodigoSFK(rs.getString(3));
					comp.setCodigoFabricante(rs.getString(4));
					comp.setPaisOrigen(rs.getString(5));
					comp.setMarca(rs.getString(6));
					comp.setCaracteristicas(rs.getString(7));
					comp.setPrecioCalculado(rs.getFloat(8));
					comp.setStockDisp(rs.getInt(9));

					comparativas.add(comp);
					
				}
		} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		}
		 PrintWriter out = response.getWriter();
		 String title = "Comparativa por Codigo Fabricante y Marca";
	 		 
		 out.println("DOCTYPE \n" + "<HTML>\n" +
	                "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" +
	                "<BODY>\n" +
	                "<H1>" + title + "</H1>\n" + 
	                "<TABLE BORDER=1 ALIGN=CENTER>\n" +
	                "<TR BGCOLOR=\"#FFAD00\">\n" +
	                "<TD>ID Proveedor</TD>" + 
	                "<TD>ID Lista</TD>" + 
	                "<TD>Codigo SFK</TD>" + 
	                "<TD>Codigo Fabricante</TD>" + 
	                "<TD>Pais Origen</TD>" + 
	                "<TD>Marca</TD>" + 
	                "<TD>Precio Calculado</TD>" + 
	                "<TD>Stock Disponible</TD>" +
		 			"<TD>Caracteristica</TD>");
		 
		for(Iterator it = comparativas.iterator(); it.hasNext(); ) { 
	        	ComparativaView c = (ComparativaView)it.next();

			 out.println("<TR>" +
					 	 "<TD>" + c.getIdProveedor() 		+ "</TD>" +
					 	 "<TD>" + c.getIdLista()     		+ "</TD>" +
					 	 "<TD>" + c.getCodigoSFK()			+ "</TD>" +
					 	 "<TD>" + c.getCodigoFabricante()   + "</TD>" +
					 	 "<TD>" + c.getPaisOrigen()   		+ "</TD>" +
					 	 "<TD>" + c.getMarca()   			+ "</TD>" +
					 	 "<TD>" + c.getPrecioCalculado() 	+ "</TD>" +
					 	 "<TD>" + c.getStockDisp() 			+ "</TD>" +
					 	 "<TD>" + c.getCaracteristicas()+   "</TD>");
		 }
		 out.println("</TABLE></BODY></HTML>");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
